﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using perfume.Models.DB;

namespace perfume.Controllers
{
    public class CheckOutController : Controller
    {
        perfume_dbEntities per_db = new perfume_dbEntities();

         //
        // GET: /Checkout/AddressAndPayment
        [Authorize]
        public ActionResult Send_export()
        {
            return View();
        }

        //
        // POST: /Checkout/AddressAndPayment
        [HttpPost]
        [Authorize]
        public ActionResult Send_export(export_check export)
        {            
            string username=Session["username"].ToString();
            var cart = ShoppingCart.GetCart(this.HttpContext);
            if (ModelState.IsValid)
            {
                TryUpdateModel(export);
                if (username != null)
                    export.username = username;
                export.export_check_time = DateTime.Now;
                export.total = Convert.ToInt32(cart.GetTotal());
                export.status = "chưa thanh toán";

                per_db.export_check.AddObject(export);
                per_db.SaveChanges();
                cart.CreateExport(export);
                return
                    RedirectToAction("Index", "Home");
            }
            return View(export);
        }

        //
        // GET: /Checkout/Complete

        public ActionResult Complete(string _username)
        {
            // Validate Account owns this order
            string username = Session["username"].ToString();
            bool isValid = per_db.export_check.Any(
                e => e.username == _username &&
                    e.user.username == _username);

            if (isValid)
            {
                return View(_username);
            }
            else
            {
                return View("Error");
            }
        }
    }
}
